package com.xjscrm.console.chat.queue;

import cn.hutool.core.date.DateUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.concurrent.DelayQueue;

/**
 *
 */
@Slf4j
public class ChatDelayProducer {

    public static final ChatDelayProducer instance = new ChatDelayProducer();

    @Getter
    private static final DelayQueue<ChatDelayTask> sendDelayQueue = new DelayQueue<>();
    @Getter
    private static final DelayQueue<ChatDelayTask> recvDelayQueue = new DelayQueue<>();
    @Getter
    private static final DelayQueue<ChatDelayTask> cleanDelayQueue = new DelayQueue<>();

    private ChatDelayProducer() {
    }

    public synchronized void addSendDelayTask(ChatDelayTask delayTask) {
        if (Objects.nonNull(delayTask)) {
            if (sendDelayQueue.contains(delayTask)) {
                log.info("添加客服回复超时任务失败, 队列中已存在相同的任务");
                sendDelayQueue.remove(delayTask);
            }
            Long delay = delayTask.getDelay();
            log.info("添加客服回复超时任务, 执行时间: {}, key: {}", DateUtil.date(delay), delayTask.getStaffId() + "-" + delayTask.getTalkSessionId());
            sendDelayQueue.offer(delayTask);
        }
    }

    public synchronized void addRecvDelayTask(ChatDelayTask delayTask) {
        if (Objects.nonNull(delayTask)) {
            if (recvDelayQueue.contains(delayTask)) {
                log.info("添加客户回复超时任务失败, 队列中已存在相同的任务");
                sendDelayQueue.remove(delayTask);
            }
            Long delay = delayTask.getDelay();
            log.info("添加客户回复超时任务, 执行时间: {}, key: {}", DateUtil.date(delay), delayTask.getStaffId() + "-" + delayTask.getTalkSessionId());
            recvDelayQueue.offer(delayTask);
        }
    }

    public synchronized void addCleanDelayTask(ChatDelayTask delayTask) {
        if (Objects.nonNull(delayTask)) {
            if (cleanDelayQueue.contains(delayTask)) {
                log.info("添加清除会话延迟任务失败, 队列中已存在相同的任务");
                sendDelayQueue.remove(delayTask);
            }
            Long delay = delayTask.getDelay();
            log.info("添加清除会话延迟任务, 执行时间: {}, key: {}", DateUtil.date(delay), delayTask.getStaffId() + "-" + delayTask.getTalkSessionId());
            cleanDelayQueue.offer(delayTask);
        }
    }

    public synchronized boolean removeSendTask(ChatDelayTask delayTask) {
        if (Objects.nonNull(delayTask) && sendDelayQueue.contains(delayTask)) {
            log.info("删除客服回复超时的延迟任务, key: {}", delayTask.getStaffId() + "-" + delayTask.getTalkSessionId());
            return sendDelayQueue.remove(delayTask);
        }
        return false;
    }

    public synchronized boolean removeRecvTask(ChatDelayTask delayTask) {
        if (Objects.nonNull(delayTask) && recvDelayQueue.contains(delayTask)) {
            log.info("删除客户回复超时的延迟任务, key: {}", delayTask.getStaffId() + "-" + delayTask.getTalkSessionId());
            return recvDelayQueue.remove(delayTask);
        }
        return false;
    }

    public synchronized boolean removeCleanTask(ChatDelayTask delayTask) {
        if (Objects.nonNull(delayTask) && cleanDelayQueue.contains(delayTask)) {
            log.info("删除清理会话的延迟任务, key: {}", delayTask.getStaffId() + "-" + delayTask.getTalkSessionId());
            return cleanDelayQueue.remove(delayTask);
        }
        return false;
    }
}
